allwinner: Introduce GPIO helper function
authorAndre Przywara <[email protected]>
Sun, 14 Oct 2018 11:03:23 +0000 (12:03 +0100)
committerAndre Przywara <[email protected]>
Sat, 20 Oct 2018 15:23:59 +0000 (16:23 +0100)
Many boards without a dedicated PMIC contain simple regulators, which
can be controlled via GPIO pins.

To later allow turning them off easily, introduce a simple function to
configure a given pin as a GPIO out pin and set it to the desired level.

Signed-off-by: Andre Przywara <[email protected]>
plat/allwinner/common/include/sunxi_private.h
plat/allwinner/common/sunxi_common.c

index 9d20f607e27f079766362c4b89e468cdec0ca6c2..54cc27e4fdbd106eaeb85a125e532c5b4d6dfd55 100644 (file)
@@ -18,5 +18,6 @@ int sunxi_pmic_setup(uint16_t socid);
 void sunxi_security_setup(void);
 
 uint16_t sunxi_read_soc_id(void);
+void sunxi_set_gpio_out(char port, int pin, bool level_high);
 
 #endif /* SUNXI_PRIVATE_H */
index ea77afb564e63a6dea79fd3fd340902ede4ffa6c..88156b51cbe9be7d77fe93efabfc7f10e4fd5edf 100644 (file)
@@ -71,3 +71,32 @@ uint16_t sunxi_read_soc_id(void)
 
        return reg >> 16;
 }
+
+/*
+ * Configure a given pin to the GPIO-OUT function and sets its level.
+ * The port is given as a capital letter, the pin is the number within
+ * this port group.
+ * So to set pin PC7 to high, use: sunxi_set_gpio_out('C', 7, true);
+ */
+void sunxi_set_gpio_out(char port, int pin, bool level_high)
+{
+       uintptr_t port_base;
+
+       if (port < 'A' || port > 'L')
+               return;
+       if (port == 'L')
+               port_base = SUNXI_R_PIO_BASE;
+       else
+               port_base = SUNXI_PIO_BASE + (port - 'A') * 0x24;
+
+       /* Set the new level first before configuring the pin. */
+       if (level_high)
+               mmio_setbits_32(port_base + 0x10, BIT(pin));
+       else
+               mmio_clrbits_32(port_base + 0x10, BIT(pin));
+
+       /* configure pin as GPIO out (4(3) bits per pin, 1: GPIO out */
+       mmio_clrsetbits_32(port_base + (pin / 8) * 4,
+                          0x7 << ((pin % 8) * 4),
+                          0x1 << ((pin % 8) * 4));
+}